package 数据结构;

import java.util.*;

public class TestIterator {
    public static void main(String[] args) {

        testIteratorList();
    }

    /**list与set方法相同，不再赘述*/
    public static void testIteratorList() {
        List<String> list = new ArrayList<>();
        list.add("aa");
        list.add("ca");
        list.add("ba");
        Collections.sort(list);
        Collections.reverse(list);

        //.hasNext()、.next()
        for (Iterator<String> iterator = list.iterator(); iterator.hasNext(); ) {
            String temp = iterator.next();
            System.out.println(temp);
        }

    }

    /**遍历map*/
    public static void testIteratorMap(){
        Map<Integer, String> map = new HashMap<>();
        map.put(10,"xx");
        map.put(12,"qq");
        map.put(11,"ff");
        //方法一
        Set<Integer> keys = map.keySet();
        for (Integer key : keys){
            System.out.println(map.get(key));
        }


       /*
       //方法二（建议）
       Set<Map.Entry < Integer, String>> entrySet = map.entrySet();
        for (Map.Entry<Integer, String> entry :entrySet) {
            System.out.println(entry.getKey() +"--"+entry.getValue());
        }*/

    }
}
